import requests
import json

start_url = "https://api.bilibili.com/playurl?aid=20840528&platform=html5"
headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:58.0) Gecko/20100101 Firefox/58.0",
    "Host": "api.bilibili.com",
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
    "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
    "Accept-Encoding": "gzip, deflate, br",
    "Connection": "keep-alive",
    "Upgrade-Insecure-Requests": "1",
    "Cache-Control": "max-age=0",
    "Cookie": "buvid3=A58167E0-3C55-4BBA-9CD2-72198DA3749C12261infoc; LIVE_BUVID=AUTO7515214652595449; finger=dbd0cac5; sid=lohumzlz"
}

res = requests.get(start_url, headers=headers)

result = json.loads(res.content.decode())

mp4_url = result["durl"][0]["url"]

res_mp4 = requests.get(mp4_url, headers=headers)

with open("mp4.mp4", "wb") as f:
    f.write(res_mp4.content)